//
//  TransferInViewController.m
//  TenMinDemo
//
//  Created by Shawn Li on 2021/3/8.
//  Copyright © 2021 CYXiang. All rights reserved.
//

#import "TransferInViewController.h"
#import "TransferRecordViewController.h"
#import "TopUpPromptView.h"
#import "CYXBaseRequest.h"
#import "QRCodeTool.h"

@interface TransferInViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *qrCode;
@property (weak, nonatomic) IBOutlet UILabel *addrLabel;
@property (weak, nonatomic) IBOutlet UILabel *infoMsgLabel;
@property (weak, nonatomic) IBOutlet UIImageView *addrImg;

/// <#注释#>
@property (nonatomic, strong) NSString *coinAddr;
@end

@implementation TransferInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setRightNavItem:LCLocalString(@"log_jl") font:[UIFont boldSystemFontOfSize:14] color:[UIColor colorWithHexString:@"#935bff"] icon:nil];
//    [self showPrompt];

    
    [self getCoinDetail];
}

- (void)showPrompt{
    TopUpPromptView *view = [[TopUpPromptView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT)];
    LRWeakSelf(self);
    [view showInView:self.navigationController.view];
}

- (void)setupUI{
    [super setupUI];
    self.title = [NSString stringWithFormat:@"%@ %@",LCLocalString(@"transfer_in"), self.coinM.name];
    self.addrLabel.text = @"";
    
    if ([self.coinM.name containsString:@"ETH"]) {
        self.infoMsgLabel.text = LCLocalString(@"in_pop_msg_ETH_USDT_ERC20");
    }else{
        self.infoMsgLabel.text = LCLocalString(@"in_pop_msg_TRX_USDT_TRC20");
    }
    
    
}

- (void)rightNavItemClick:(id)sender{
    TransferRecordViewController *vc = LoadVCFormXib(TransferRecordViewController);
    vc.coinM = self.coinM;
    vc.listType = 1;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (IBAction)copyAction:(id)sender {
   
//    view.doneBlock = ^{
//        [weakself showTransferOutDone];
//    };
    LCCopyBoard(self.coinAddr);
    LCInfoHUD(@"地址已经复制", 1);
   
}

- (void)getCoinDetail{
        NSDictionary *param = @{@"assetId":@(self.coinM.id)};
    
        [CYXBaseRequest postWithUrl:API_getAssetDetail param:param resultClass:nil success:^(id res) {
            if ([res isEqual: RequestErrorCode]) {
                
            }else{
                self.coinAddr = res[@"address"];
                [self showQRCode];
            }
            
            CYXLog(@"%@",res)
        } failure:^(NSError *error) {
            CYXLog(@"%@",error)
        }];
}

- (void)showQRCode{
    if (!self.coinAddr.length) {
        return;
    }
    self.addrLabel.text = self.coinAddr;
    self.addrImg.image = [QRCodeTool getQRCodeImg:self.coinAddr];
}
- (IBAction)savePic:(id)sender {
    if (self.qrCode.image) {
        UIImageWriteToSavedPhotosAlbum(self.qrCode.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
}

#pragma mark -- <保存到相册>
-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSString *msg = nil ;
    if(error){
        msg = @"保存图片失败" ;
    }else{
        msg = @"保存图片成功" ;
    }
    LCInfoHUD(msg, 1.5);
}



@end
